package ${packageName};


import com.alibaba.fastjson.JSONObject;
import com.changgou.entity.MessageConstant;
import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.exceptionResolver.ExceptionMessage;

import com.changgou.pojo.${pojoNameBig};

import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@RequestMapping("/${pojoNameLte}")
@RestController
public class ${pojoNameBig}Controller {

@Autowired
${pojoNameBig}Service ${pojoNameLte}Service;

/**
* 查询所有
*
* @return
*/
@GetMapping("/findAll")
public Result findAll() {
List<${pojoNameBig}> ${pojoNameLte}s = ${pojoNameLte}Service.findAll();


return new Result(true, 200, "成功", ${pojoNameLte}s);
}

@GetMapping("/findById/{id}")
public Result findById(@PathVariable Long id) {
${pojoNameBig} ${pojoNameLte} = ${pojoNameLte}Service.findById(id);
return new Result(true, StatusCode.OK, MessageConstant.SEARCH_SUCCESS, ${pojoNameLte});
}


/**
* 根据条件查询
*
* @param currentPage
* @param pageSize
* @param query       查询条件
* @return
*/
@PostMapping("/findPage/{currentPage}/{pageSize}")
@ExceptionMessage(MessageConstant.SEARCH_FALL)
public Result findPage(@PathVariable("currentPage") int currentPage, @PathVariable("pageSize") int pageSize, @RequestBody(required = false) Map
<String, Object> query) {
Page<${pojoNameBig}> page = ${pojoNameLte}Service.findPage(currentPage, pageSize, query);
PageResult<${pojoNameBig}> ${pojoNameLte}PageResult = new PageResult<>(page.getTotal(), page.getResult());
return new Result(true, StatusCode.OK, MessageConstant.SEARCH_SUCCESS, ${pojoNameLte}PageResult);
}

@PostMapping("/add")
@ExceptionMessage(value = MessageConstant.ADD_FALL)
public Result add(@RequestBody ${pojoNameBig} ${pojoNameLte}) {
${pojoNameLte}Service.add(${pojoNameLte});
return new Result(true, StatusCode.OK, MessageConstant.ADD_SUCCESS);
}

@PutMapping("/update")
@ExceptionMessage(MessageConstant.UPDATE_FALL)
public Result update(@RequestBody ${pojoNameBig} ${pojoNameLte}) {
${pojoNameLte}Service.update(${pojoNameLte});
return new Result(true, StatusCode.OK, MessageConstant.UPDATE_SUCCESS);
}


/**
* RestFul风格批量删除设计
*
* @param idsJson
* @return
*/
@DeleteMapping("/delete")
public Result deleteByIds(@RequestBody JSONObject idsJson) {
Long[] ids = idsJson.getObject("ids", Long[].class);
${pojoNameLte}Service.deleteByIds(ids);
return new Result(true, StatusCode.OK, MessageConstant.DELETE_SUCCESS);
}

}
